package com.Action;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import transformFactory.CaculatePregnentWeek;

import com.Entity.GesWeeks;
import com.Entity.Userdata;
import com.Entity.Users;
import com.Service.GesWeeksService;
import com.Service.UserdataService;
import com.opensymphony.xwork2.ActionSupport;


public class WeekKnowledgeAction extends ActionSupport{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final Logger logger = Logger.getLogger(WeekKnowledgeAction.class);
	
	private static String ERRO="erro";
	private static String SUCCESS="success";
	private int weekid=0;
	private int userid=0;
	private Date pregnancydate; 

	@Autowired
	private GesWeeksService gesweeksservice;
	@Autowired
	private UserdataService userdataservice;
	
	private Users user;
	
	private GesWeeks gesweek;
	
	

	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}

	public GesWeeks getGesweek() {
		return gesweek;
	}

	public void setGesweek(GesWeeks gesweek) {
		this.gesweek = gesweek;
	}

	public int getWeekid() {
		return weekid;
	}

	public void setWeekid(int weekid) {
		this.weekid = weekid;
	}

	public int getUserid() {
		return userid;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}


	
	public String getWeekKnowledge() throws Exception{
		userid=Integer.parseInt(ServletActionContext.getRequest().getParameter("userid"));
		Userdata userdata=this.userdataservice.getById(userid);	
		//logger.info(userdata.getPregnancyDate());
		if(userdata.getPregnancyDate()==null||userdata.getPregnancyDate().equals("")){
			return "setPregnancyDate";
		}
		else{			
			weekid=CaculatePregnentWeek.CaculateWeek(userdata.getPregnancyDate());
			//logger.info("weekid:---"+weekid);
			gesweek=this.gesweeksservice.getById(weekid);
			return SUCCESS;
		}

	}
	HttpSession session = ServletActionContext.getRequest().getSession();

	public String getWeekKnowledge2() throws Exception{	
		user=(Users) session.getAttribute("temploginbean");
		this.userdataservice.updatePregnancyDate(pregnancydate,user.getUserId());
		weekid=CaculatePregnentWeek.CaculateWeek(pregnancydate);
		gesweek=this.gesweeksservice.getById(weekid);
		return SUCCESS;
	}
	
	public String getWeekKnowledge3()throws Exception{
		weekid= Integer.parseInt(ServletActionContext.getRequest().getParameter("weekid"));
		gesweek=this.gesweeksservice.getById(weekid);
		return SUCCESS;
	}
	

	public Date getPregnancydate() {
		return pregnancydate;
	}

	public void setPregnancydate(Date pregnancydate) {
		this.pregnancydate = pregnancydate;
	}

}
